package lily.phs.entertain.llk;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import lily.phs.entertain.llk.CtrlView.Point;
import lily.phs.urh.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class GameView extends View {

	public final int row = 10;
	public final int col = 10;
	public float width;
	public float height;
	public int isLine = 0;
	public boolean tip = false;
	
	public int grid[][] = new int[row][col];//格子属性
	public int Relationship[][];//药材之间的关系
	
	public Rect selRect = new Rect();//第一个选择的格子
	public Rect selRect2 = new Rect();//第二个选择的格子
	public int selY;
	public int selX;
	
	public int lineType = 0;
	public final int V_LINE = 1;
	public final int H_LINE = 1;
	public final int ONE_C_LINE = 2;
	public final int TWO_C_LINE = 3;
	public int much = 0;
	Point[] p;//路径的拐点集
	
	//药品图片
	public int[] imageType = new int[] { 
			R.drawable.gansu01_danggui, R.drawable.gansu02_dahuang, R.drawable.gansu03_dangsen, R.drawable.gansu04_huangshi, 
			R.drawable.gansu05_hongshi, R.drawable.gansu06_gancao, R.drawable.gansu07_caihu, R.drawable.gansu08_zhuling,
			R.drawable.gansu09_banxia, R.drawable.gansu10_kuandonghua, R.drawable.gansu11_kusen, R.drawable.gansu12_xingren,
			R.drawable.gansu13_digupi, R.drawable.gansu14_ciwujia, R.drawable.gansu15_chishao, R.drawable.gansu16_yuanzhi};
	
	public Bitmap[] image;
	
	//药品名称，与图片的先后顺序对应
	public static final String[] NameType = {"当归","大黄","党参","黄芪","红芪","甘草","柴胡","猪苓","半夏","款冬花","苦参","杏仁","地骨皮","刺五加","赤芍","远志"};
		
	public List<Integer> type = new ArrayList<Integer>();

	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setFocusable(true);
		this.setFocusableInTouchMode(true);
		Relationship = initRelationship();
	}

	

	public GameView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setFocusable(true);
		this.setFocusableInTouchMode(true);
		Relationship = initRelationship();
	}

	public void reset() {

	}

	public void fillImage(Context context) {
		int lth = imageType.length;
		image = new Bitmap[lth];
		for (int i = 0; i < lth; i++) {
			Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height,
					Bitmap.Config.ARGB_8888);
			Drawable drw;
			Canvas canvas = new Canvas(bitmap);
			drw = context.getResources().getDrawable(imageType[i]);
			drw.setBounds(1, 1, 50, 50);
			drw.draw(canvas);
			image[i] = bitmap;
		}
	}

	

	public void select(int x, int y, Rect rect) {
		invalidate(rect);
		selX = Math.min(Math.max(x, 0), 9);
		selY = Math.min(Math.max(y, 0), 9);
		getRect(selX, selY, rect);
		invalidate(rect);
	}

	public void getRect(int x, int y, Rect rect) {
		rect.set((int) (x * width), (int) (y * height),
				(int) (x * width + width), (int) (y * height + height));
	}
	
	public int[][] initRelationship() {
		// TODO Auto-generated method stub
		int sizeR = imageType.length;
		int tempR[][] = new int[sizeR][sizeR]; 
		
		/*****************************************************/
		//为简单起见，药材之间的关系先随机产生，以后再作具体的修改:
		//1-可以一起使用；0-会产生副作用
		/*****************************************************/
		for(int i=0;i<sizeR;i++){
			for(int j=0;j<sizeR;j++){
				if(i==j){
					tempR[i][j] = 1;
				}
				
				if(i<j){
					int k = (int)(Math.random()*100);
					tempR[i][j] = k%2;
					tempR[j][i] = tempR[i][j];
				}
				
			}
		}
		return tempR;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint background = new Paint();
		background.setColor(Color.WHITE);
		canvas.drawRect(0, 0, getWidth(), getHeight(), background);
		Paint hilite = new Paint();
		hilite.setColor(getResources().getColor(R.color.hilite));
		Paint light = new Paint();
		light.setColor(getResources().getColor(R.color.light));
//		System.out.println(imageType.toString());
		
		//布局10x10
		for (int i = 0; i <= 9; i++) {
			canvas.drawLine(0, i * height, getWidth(), i * height, light);
			canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
					hilite);
			canvas.drawLine(i * width, 0, i * width, getHeight(), light);
			canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
					hilite);
		}
		

		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (grid[i][j] != 0) {
					int temp = Arrays.binarySearch(imageType, grid[i][j]);
//					System.out.println("search image:grid["+i+"]["+j+"]="+grid[i][j]);
//					System.out.println("search image="+temp);
					canvas.drawBitmap(image[temp], i * width, j * height, null);
				}
			}
		}

		if (CtrlView.CURRENT_CH) {
			Paint selected = new Paint();
			selected.setColor(getResources().getColor(R.color.puzzle_selected));
			selected.setAntiAlias(true);
			selected.setStyle(Style.STROKE);
			canvas.drawRect(selRect, selected);
		}
		
		if (CtrlView.CURRENT_CH2) {
			Paint selected = new Paint();
			selected.setColor(getResources().getColor(R.color.puzzle_selected));
			selected.setAntiAlias(true);
			selected.setStyle(Style.STROKE);
			canvas.drawRect(selRect2, selected);
		}

		if (isLine==1) {//两药材可以一起使用//相生
			Paint line = new Paint();
			line.setColor(Color.GREEN);
			drawLines(canvas,line);
			
		}
		else if(isLine==2){//药材间会产生副作用//相克
			Paint line = new Paint();
			line.setColor(Color.RED);
			drawLines(canvas,line);
			
		}
		super.onDraw(canvas);
	}

	private void drawLines(Canvas c, Paint lineColor) {
		// TODO Auto-generated method stub
		switch (lineType) {
		case V_LINE:
			c.drawLine(p[0].x * width + width / 2, p[0].y * height
					+ height / 2, p[1].x * width + width / 2, p[1].y
					* height + height / 2, lineColor);
			break;
		case ONE_C_LINE:
			c.drawLine(p[0].x * width + width / 2, p[0].y * height
					+ height / 2, p[1].x * width + width / 2, p[1].y
					* height + height / 2, lineColor);
			c.drawLine(p[1].x * width + width / 2, p[1].y * height
					+ height / 2, p[2].x * width + width / 2, p[2].y
					* height + height / 2, lineColor);
			break;
		case TWO_C_LINE:
			c.drawLine(p[0].x * width + width / 2, p[0].y * height
					+ height / 2, p[1].x * width + width / 2, p[1].y
					* height + height / 2, lineColor);
			c.drawLine(p[1].x * width + width / 2, p[1].y * height
					+ height / 2, p[2].x * width + width / 2, p[2].y
					* height + height / 2, lineColor);
			c.drawLine(p[3].x * width + width / 2, p[3].y * height
					+ height / 2, p[2].x * width + width / 2, p[2].y
					* height + height / 2, lineColor);
			break;
		default:
			break;
		}
		
	}



	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		width = w / row;
		height = h / col;
		// getRect(1,1,selRect);
		fillImage(this.getContext());
		super.onSizeChanged(w, h, oldw, oldh);
	}
	
	public void initType() {
		int size = (row - 2) * (col - 2);
		int count = size / imageType.length;
		for (int j = 0; j < imageType.length; j++) {
			for (int i = 0; i < count; i++) {
				type.add(imageType[j]);
//				System.out.println(imageType[j]);
			}
		}
	}

	public void initGrid() {
		Random ad = new Random();
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (i == 0 || i == row - 1 || j == 0 || j == col - 1) {
					grid[i][j] = 0;
				} else {
					if (type != null && type.size() > 0) {
						int index = ad.nextInt(type.size());
						grid[i][j] = type.get(index);
//						System.out.println("grid["+i+"]["+j+"]="+grid[i][j]);
//						type.remove(index);
					}
				}
			}
		}
	}

}
